﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityModel;
using System.Data.Linq;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCust_ContactPersonDAL:ItblCust_ContactPerson
    {
        HKEA_CMSEntities model;
        tblCust_ContactPerson person;

        public tblCust_ContactPersonDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCust_ContactPersonDAL GetInstance()
        {
            return new tblCust_ContactPersonDAL();
        }

        public int InsertENT(tblCust_ContactPerson ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCust_ContactPerson.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblCust_ContactPerson> GetListByENT(tblCust_ContactPerson ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCust_ContactPerson.Where(a => a.ContactPersonUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);           
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<M_ContactPersonListDetail> GetCPListDetail(List<int> ids, int custUID)
        {
            List<M_ContactPersonListDetail> list = new List<M_ContactPersonListDetail>();

            var ret = from cust_cp in model.tblCust_ContactPerson
                      where cust_cp.CustomerUID == custUID && cust_cp.Deleted == 0
                      select cust_cp into cpList
                      join cp in model.tblContactPersons
                      on cpList.ContactPersonUID equals cp.ContactPersonUID

                      join info in model.tblContactInfoes
                      on cp.ContactInfoUID equals info.ContactInfoUID into infoENT
                      from info in infoENT.DefaultIfEmpty()

                      join address in model.tblAddresses
                      on cp.AddressUID equals address.AddressUID into addressENT
                      from address in addressENT.DefaultIfEmpty()

                      join salutation in model.mtblSalutations
                      on cp.SalutationUID equals salutation.SalutationUID into tempSalutation
                      from salutation in tempSalutation.DefaultIfEmpty()
                      select new M_ContactPersonListDetail
                      {
                          IsFromApp = false,
                          AddressUID = (int)cp.AddressUID,
                          ChristianName = cp.ChristianName,
                          ContactPersonUID = cp.ContactPersonUID,
                          CustContactUID = cpList.CustContactUID,
                          FirstName_Chi = cp.FirstName_Chi,
                          LastName_Chi = cp.LastName_Chi,
                          InfoUID = (int)cp.ContactInfoUID,
                          OtherTitle_Chi = cp.OtherTitle_Chi,
                          OtherTitle_Eng = cp.OtherTitle_Eng,
                          Position_Chi = cpList.Position_Chi,
                          Position_Eng = cpList.Position_Eng,
                          Salutation = cp.mtblSalutation.Salutation_Eng,
                          FirstName_Eng=cp.FirstName_Eng,
                          LastName_Eng=cp.LastName_Eng
                      };
            if (ids.Count > 0)
                ret = ret.Where(a =>!ids.Contains(a.CustContactUID));
            list = ret.ToList();
            return list;
        }

        public List<ContactPersonListDetail> GetCPListDetail(int custUID)
        {
            List<ContactPersonListDetail> list = new List<ContactPersonListDetail>();

            var ret = from cust_cp in model.tblCust_ContactPerson
                      where cust_cp.CustomerUID == custUID && cust_cp.Deleted == 0
                      select cust_cp into cpList
                      join cp in model.tblContactPersons
                      on cpList.ContactPersonUID equals cp.ContactPersonUID

                      join info in model.tblContactInfoes
                      on cp.ContactInfoUID equals info.ContactInfoUID into infoENT
                      from info in infoENT.DefaultIfEmpty()

                      join address in model.tblAddresses
                      on cp.AddressUID equals address.AddressUID into addressENT
                      from address in addressENT.DefaultIfEmpty()

                      join salutation in model.mtblSalutations
                      on cp.SalutationUID equals salutation.SalutationUID into tempSalutation
                      from salutation in tempSalutation.DefaultIfEmpty()
                      select new ContactPersonListDetail
                      {
                          Salutation=salutation.Salutation_Eng,
                          FirstName_Eng = cp.FirstName_Eng,
                          LastName_Eng=cp.LastName_Eng,
                          ChristinaName = cp.ChristianName,
                          Position_Chi = cpList.Position_Eng,
                          Position_Eng=cpList.Position_Chi,
                          CPENT=cp,
                          CPUID=cp.ContactPersonUID,
                          FullAdd_Eng = address.FullAdd_Eng,
                          FullAdd_Chi = address.FullAdd_Chi,
                          infoUID = info.ContactInfoUID,
                          ContactInfo=info
                      };
            list = ret.ToList();
            
            return list;
        }

        public List<tblCust_ContactPerson> GetListByENT(tblCust_ContactPerson ent)
        {
            var result = model.tblCust_ContactPerson.Where(a => a.ContactPersonUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if(ent.Deleted>=0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (ent.CustContactUID > 0)
                result = result.Where(a=>a.CustContactUID==ent.CustContactUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblCust_ContactPerson ent)
        {
            var result = model.tblCust_ContactPerson.Where(a => a.ContactPersonUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCust_ContactPerson ent)
        {
            person = model.tblCust_ContactPerson.Where(a=>a.ContactPersonUID==ent.ContactPersonUID).FirstOrDefault();
            if (person != null)
            {
                person.Deleted = ent.Deleted;
                person.LastModifyBy = ent.LastModifyBy;
                person.LastModifyDate = DateTime.Now;
                person.CustContactUID = ent.CustContactUID;
                person.CustomerUID = ent.CustomerUID;
                person.Position_Chi = ent.Position_Chi;
                person.Position_Eng = ent.Position_Eng;
                person.SourceUID = ent.SourceUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblCust_ContactPerson ent)
        {
            var temp = model.tblCust_ContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                temp = temp.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCust_ContactPerson.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
